﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Stripe;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly BitDbContext _db;
        public AccountController(BitDbContext db)
        {
            _db = db;
        }
        
        #region 增
        [HttpPost]
        public Result Add([FromBody] EM_Account account)
        {
            Result result = new Result();
            _db.EM_Account.Add(account);
            result.Data = _db.SaveChanges();
            return result;
        }
        #endregion

        #region 删
        [HttpDelete]
        public Result Del(Guid id)
        {
            Result result = new Result();
            EM_Account? account = _db.EM_Account.Where(w => w.Id == id).SingleOrDefault();
            if (account != null)
                _db.EM_Account.Remove(account);
            result.Data = _db.SaveChanges();
            return result;
        }

        [HttpPut("del")]
        public Result IsDel([FromHeader] Guid Id)
        {
            Result result = new Result();
            EM_Account? account = _db.EM_Account.Where(w => w.Id == Id).SingleOrDefault();
            account.DelTime = DateTime.Now;
            account.IsDel = true;
            result.Data = _db.SaveChanges();
            return result;

        }
        #endregion

        #region 改
        [HttpPut]
        public Result Put([FromBody] EM_Account em_account)
        {
            Result result = new Result();
            _db.EM_Account.Update(em_account);
            result.Data = _db.SaveChanges();
            return result;
        }
        #endregion

        #region 查
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            List<EM_Account> resultdata = _db.EM_Account.ToList();
            result.Data = resultdata;
            result.Code = 200;
            return result;
        }

        //[HttpGet]
        //public Result GetBySelect([FromBody]string account,[FromBody] string phonenumber,[FromBody] string email)
        //{
        //    Result result = new Result();
        //    if(!string.IsNullOrEmpty(account))
        //    {
        //        result.Data = _db.EM_Account.Where(s => s.Account == account).OrderBy(s=>s.Account).ToList();
        //    }
        //    if (!string.IsNullOrEmpty(phonenumber))
        //    {
        //        result.Data = _db.EM_Account.Where(s => s.PhoneNumber == phonenumber).OrderBy(s => s.Account).ToList();
        //    }
        //    if (!string.IsNullOrEmpty(email))
        //    {
        //        result.Data = _db.EM_Account.Where(s => s.Email == email).OrderBy(s => s.Account).ToList();
        //    }
        //    return result;
        //}

        //[HttpGet]
        //public IQueryable GetBySelect([FromHeader] string account, [FromHeader] string phonenumber, [FromHeader] string email)
        //{
        //    IQueryable<EM_Account> query = _db.EM_Account;


        //    if (!string.IsNullOrEmpty(account))
        //    {
        //        query = _db.EM_Account.Where(s => s.Account == account);
        //    }
        //    if (!string.IsNullOrEmpty(phonenumber))
        //    {
        //        query = _db.EM_Account.Where(s => s.PhoneNumber == phonenumber);
        //    }
        //    if (!string.IsNullOrEmpty(email))
        //    {
        //        query = _db.EM_Account.Where(s => s.Email == email);
        //    }
        //    return query;
        //}

        [HttpGet("{Id}")]
        public Result GetById(Guid Id)
        {
            Result result = new Result();
            result.Data = _db.EM_Account.Where(w => w.Id == Id).SingleOrDefault();
            return result;
        }

        [HttpGet("{Account}/{Password}")]
        public Result Login(string _account, string _password)
        {
            Result result = new Result();
            EM_Account? account = _db.EM_Account.Where(w => w.Account == _account && w.Password == _password).SingleOrDefault();
            if (account != null)
            {
                result.Code = 200;
                result.Data = account;
            }
            else
            {
                result.Code = 404;
            }
            result.Author = "1823132031@qq.com";
            return result;
        }
        [HttpGet("like/{Name}")]
        public Result GetByName(string name)
        {
            Result result = new Result();
            List<EM_Account> resultdata = new List<EM_Account>();
            resultdata = _db.EM_Account.Where(w => w.Account.Contains(name)).ToList();
            result.Data = resultdata;
            return result;
        }
        #endregion



    }
}
